package com.dididoctor.doctor.Activity.Main;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dididoctor.doctor.Activity.Main.Inquiry.InquiryListActivity;
import com.dididoctor.doctor.Activity.Main.Message.MessageActivity;
import com.dididoctor.doctor.Bean.Token;
import com.dididoctor.doctor.MV.EduActivity;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Ui.MarqueeTextView;
import com.dididoctor.doctor.Utils.LoaderImage;
import com.dididoctor.doctor.Utils.UserHeadImage;
import com.dididoctor.doctor.Utils.Util;
import com.dididoctor.patient.Bean.DiaeasesLitapal;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;


/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： ConsultActivity
 * @包名称： com.dididoctor.doctor.Activity.Trends
 * @创建人： zhanjunjie
 * @创建时间：2016/4/1 15:19
 * @修改人：
 * @修改时间：2016/4/1 15:19
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class ConsultActivity extends EduActivity implements ConsultView {


    //控件
    private LinearLayout mLlImgOrText, mLlCall, mlinOnline, mlinFamilDoctor, mllTwoCode, mllInquiry, mllMessage,mLinisok;
    private TextView mTvName, mTvJob, mTVSige, mTvTwoName, mTvTwoJob, mTvOnline, mTvFamilyDoctor, mTvCallCounsel;
    private MarqueeTextView mMtvpass;
    private UserHeadImage headImage, twoloaderImage;
    private LoaderImage loaderImage;
    private RelativeLayout mRlTwoCode;
    private ImageView mImgTwoCode;

    private ConsultPresenter presenter;
    private List<DiaeasesLitapal> litapals = new ArrayList<>();

    private String onlinestatus = "", callstatus = "", famirystatus = "";
    private Consult consult = new Consult();

    @Override
    public void init() {
        //初始化控件
        initview();

//        initdata();
    }

    private void initdata() {
        presenter = new ConsultPresenter(this, this);
        presenter.getdoctorInfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter = new ConsultPresenter(this, this);
        presenter.getdoctorInfo();

        if ("4".equals(Token.getIsPass())) {
            mLinisok.setVisibility(View.VISIBLE);
            mMtvpass.setText("很抱歉您没有通过滴滴医生平台认证，您需要再次提交认证信息认证");
        } else if ("2".equals(Token.getIsPass())) {
            mLinisok.setVisibility(View.VISIBLE);
            mMtvpass.setText("你的医生资格处于审核状态，部分功能无法使用，您可以联系客服办理加速审核");
        } else {
            mLinisok.setVisibility(View.GONE);
        }

    }

    private void initview() {
        mTvName = (TextView) findViewById(R.id.tv_name);
        mTvJob = (TextView) findViewById(R.id.tv_job);
        mTVSige = (TextView) findViewById(R.id.tv_sigeorpin);
        //上线
        mlinOnline = (LinearLayout) findViewById(R.id.lin_online);
        mTvOnline = (TextView) findViewById(R.id.tv_online);
        mlinOnline.setOnClickListener(this);
        //图文
        mLlImgOrText = (LinearLayout) findViewById(R.id.lin_imgortext);
        mLlImgOrText.setOnClickListener(this);
        //电话
        mLlCall = (LinearLayout) findViewById(R.id.lin_call_counsel);
        mTvCallCounsel = (TextView) findViewById(R.id.tv_call_counsel);
        mLlCall.setOnClickListener(this);
        //家庭医生
        mlinFamilDoctor = (LinearLayout) findViewById(R.id.lin_family_doctor);
        mTvFamilyDoctor = (TextView) findViewById(R.id.tv_family_doctor);
        mlinFamilDoctor.setOnClickListener(this);
        //我的二维码
        mllTwoCode = (LinearLayout) findViewById(R.id.ll_two_code);
        mllTwoCode.setOnClickListener(this);
        //问诊表
        mllInquiry = (LinearLayout) findViewById(R.id.ll_inquiry);
        mllInquiry.setOnClickListener(this);
        //消息
        mllMessage = (LinearLayout) findViewById(R.id.ll_message);
        mllMessage.setOnClickListener(this);
        mMtvpass = (MarqueeTextView) findViewById(R.id.tv_ispass);

        mLinisok = (LinearLayout) findViewById(R.id.lin_isok);

        headImage = (UserHeadImage) findViewById(R.id.img_user);
        twoloaderImage = (UserHeadImage) findViewById(R.id.img_two_user);
        mTvTwoName = (TextView) findViewById(R.id.tv_two_name);
        mTvTwoJob = (TextView) findViewById(R.id.tv_two_job);
        mImgTwoCode = (ImageView) findViewById(R.id.img_two_code);
        loaderImage = new LoaderImage(this);

        mRlTwoCode = (RelativeLayout) findViewById(R.id.rl_two_code);


        findViewById(R.id.lin_call_counsel).setOnClickListener(this);
        findViewById(R.id.lin_imgortext).setOnClickListener(this);
        findViewById(R.id.img_cancel).setOnClickListener(this);


    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.ll_two_code:
                mRlTwoCode.setVisibility(View.VISIBLE);
                loaderImage.load(twoloaderImage, consult.getHeadPic(), new ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String s, View view) {
                        mImgTwoCode.setImageResource(R.drawable.img_doctordefault);
                    }

                    @Override
                    public void onLoadingFailed(String s, View view, FailReason failReason) {
                        mImgTwoCode.setImageResource(R.drawable.img_doctordefault);
                    }

                    @Override
                    public void onLoadingComplete(String s, View view, Bitmap bitmap) {

                    }

                    @Override
                    public void onLoadingCancelled(String s, View view) {

                    }
                });
                mTvTwoName.setText(consult.getName());
                mTvTwoJob.setText(consult.getPosition());
                mImgTwoCode.setImageBitmap(createQRImage(consult.getHospitalId(),500,500));
                break;
            case R.id.ll_inquiry:
                Intent intent = new Intent(this, InquiryListActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_message:
                Intent intent1 = new Intent(this, MessageActivity.class);
                startActivity(intent1);
                break;
            case R.id.img_cancel:
                mRlTwoCode.setVisibility(View.GONE);
                break;
            case R.id.lin_online:
                if ("1".equals(onlinestatus)) {
                    onlinestatus = "0";
                } else {
                    onlinestatus = "1";
                }
                presenter.updatestatus("1", onlinestatus);
                break;
            case R.id.lin_call_counsel:
                if ("1".equals(callstatus)) {
                    callstatus = "0";
                } else {
                    callstatus = "1";
                }
                presenter.updatestatus("2", callstatus);
                break;
            case R.id.lin_family_doctor:
                if ("1".equals(famirystatus)) {
                    famirystatus = "0";
                } else {
                    famirystatus = "1";
                }
                presenter.updatestatus("3", famirystatus);
                break;
        }
    }

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_consult);
    }

    @Override
    public void getdoctorsucced(Consult consult) {
        this.consult = consult;
        onlinestatus = consult.getOnline();
        updateviewstatus("1", onlinestatus);
        callstatus = consult.getCallSwitch();
        updateviewstatus("2", callstatus);
        famirystatus = consult.getIsSign();
        updateviewstatus("3", famirystatus);
        mTvName.setText(consult.getName());
        mTvJob.setText(consult.getPosition());
        String sign = Util.isEmpty(consult.getSignNum()) ? "0" : consult.getSignNum();
        String evlaute = Util.isEmpty(consult.getEvaluate()) ? "100" : consult.getEvaluate();
        mTVSige.setText("签约数：" + sign + "       " + "好评率：" + evlaute + "%");
        loaderImage.load(headImage, consult.getHeadPic(), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {
                headImage.setImageResource(R.drawable.img_doctordefault);
            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {
                headImage.setImageResource(R.drawable.img_doctordefault);
            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {

            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });

    }

    @Override
    public void getdoctorfail() {
        showToastMessage("加载失败");
    }

    @Override
    public void updatestatesucced(String type, String status) {
        updateviewstatus(type, status);
    }

    /**
     * 状态更新
     *
     * @param type
     * @param status
     */
    private void updateviewstatus(String type, String status) {
        if ("1".equals(type)) {//如果是上线
            if ("0".equals(status)) {
                mlinOnline.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow_with));
                mTvOnline.setTextColor(getResources().getColor(R.color.main_color));
            } else {
                mlinOnline.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow));
                mTvOnline.setTextColor(getResources().getColor(R.color.white));
            }
        } else if ("2".equals(type)) {//如果是电话
            if ("0".equals(status)) {
                mLlCall.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow_with));
                mTvCallCounsel.setTextColor(getResources().getColor(R.color.main_color));
            } else {
                mLlCall.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow));
                mTvCallCounsel.setTextColor(getResources().getColor(R.color.white));
            }
        } else if ("3".equals(type)) {//如果是医生
            if ("0".equals(status)) {
                mlinFamilDoctor.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow_with));
                mTvFamilyDoctor.setTextColor(getResources().getColor(R.color.main_color));
            } else {
                mlinFamilDoctor.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow));
                mTvFamilyDoctor.setTextColor(getResources().getColor(R.color.white));
            }
        }
    }

    @Override
    public void updatestatefail() {
        showToastMessage("失败");
    }


    /**
     * 生成二维码 要转换的地址或字符串,可以是中文
     *
     * @param url
     * @param width
     * @param height
     * @return
     */
    public Bitmap createQRImage(String url, final int width, final int height) {
        try {
            // 判断URL合法性
            if (url == null || "".equals(url) || url.length() < 1) {
                return null;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            // 图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(url,
                    BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            // 下面这里按照二维码的算法，逐个生成二维码的图片，
            // 两个for循环是图片横列扫描的结果
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * width + x] = 0xff000000;
                    } else {
                        pixels[y * width + x] = 0xffffffff;
                    }
                }
            }
            // 生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }
}
